package com.example.pt.controller;

import com.example.pt.dto.ApiResponse;
import com.example.pt.entity.Notification;
import com.example.pt.entity.Post;
import com.example.pt.entity.Reply;
import com.example.pt.repository.NotificationRepository;
import com.example.pt.repository.PostRepository;
import com.example.pt.repository.ReplyRepository;
import com.example.pt.service.NotificationService;
import com.example.pt.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/replies")
public class ReplyController {

    @Autowired
    private ReplyRepository replyRepository;
    @Autowired
    private PostRepository postRepository;
    @Autowired
    private NotificationService notificationService;
    @Autowired
    private ReplyService replyService;

    // 返回评论列表
    @PostMapping("/replyList")
    public ApiResponse replyList(@RequestBody Reply reply) {
        Long post_id = reply.getPost_id();
        List<Reply> replyList = replyRepository.findByPostId(post_id);
        return ApiResponse.success(replyList);
    }

    // 创建评论
    @PostMapping("/createReply")
    public ApiResponse createReply(@RequestBody Reply reply) {
        Reply savedReply = replyService.createReply(reply);
        return ApiResponse.success(savedReply);
    }

    //删除评论
    @PostMapping("/delete")
    public ApiResponse delete(@RequestBody Reply reply) {
        Long replyId = reply.getReply_id();
        Long userId =reply.getUser_id();
        int result = replyRepository.deleteByReplyIdAndUserId(replyId,userId);
        if (result > 0) {
            return ApiResponse.success("删除成功");
        } else {
            return ApiResponse.fail("删除失败，无删除权限");
        }
    }

}
